
import Common from './common';
import moment from 'moment';
import proFetch from '../../../../../../hrpub/common/utils/project-fetch';
import language from '../../../language';
import {pageTo} from 'nc-lightapp-front';

export default class BatchAddAction extends Common {

    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 设置批量添加弹窗第二步骤表单的申请日期默认值
    setFormApplyDate = () => {
        const {props} = this.comp;
        const {form} = props;

        form.setAllFormValue({
            entryinfo: {
                rows: [{
                    values: {
                        apply_date: {
                            value: moment().format('YYYY-MM-DD'),
                            display: moment().format('YYYY-MM-DD')
                        }
                    }
                }]
            }
        });
    }

    // 拼装表单编辑前请求的参数
    makeFormEditBeforeOption = (key) => {
        const {props} = this.comp;

        let postData = {
            key: key,
            pk_org: props.orgVal.refpk
        };

        return postData;
    }

    // 发起表单编辑前请求
    postEditBefore = async (postData) => {
        const {props, state} = this.comp;
        const {emp, dispatch, meta} = props;

        try {
            let res = await proFetch({
                body: postData,
                url: '/nccloud/hrtrn/entrymng/EntryBatchAddBeforeEditAction.do'
            });

            return res;
        }
        catch(e) {
            console.error(e);
        }
    }

    // 给模版里的字段添加queryCondition
    addQueryConditionToTemplateItem = (formId, key, queryCondition = {}) => {
        const {props} = this.comp;
        const {meta} = props;

        let template = meta.getMeta();

        template[formId].items.map((item) => {
            if(item.attrcode === key) {
                Object.assign(item.queryCondition, queryCondition);
            }
        });
    }

    // 第二步表单编辑前
    formBeforeEdit = async (fprops, moduleId, key, value, data) => {
        const {props} = this.comp;
        const {meta} = props;

        let postData = this.makeFormEditBeforeOption(key);

        try {
            let res = await this.postEditBefore(postData);

            if(res.success) {

                if(res.data && res.data.refParam) {
                    this.addQueryConditionToTemplateItem('entryinfo', key, res.data.refParam);
                }

                await new Promise((resolve, reject) => {
                    let template = meta.getMeta();
                    meta.setMeta(template, resolve);
                });
            }
        }
        catch(e) {
            console.error(`formBeforeEdit: ${e}`);
        }
        return true;
    }

    // 拼装弹窗第二步表单编辑后请求参数
    makeFormEditAfterOption = (key) => {
        const {props, state} = this.comp;
        const {form, orgVal} = props;

        let postData = {
            key: key,
            model: form.getAllFormValue('entryinfo'),
            pk_org: orgVal.refpk
        };

        return postData;
    }

    // 发起表单编辑后的请求
    postEditAfter = async (postData) => {
        try {
            let res = await proFetch({
                url: '/nccloud/hrtrn/entrymng/EntryBatchAddAfterEditAction.do',
                body: postData
            });

            return res;
        }
        catch(e) {
            console.error(e);
        }
    }

    // 批量新增弹窗第二步编辑后
    formAfterEdit = async (fprops, moduleId, key, value, data) => {
        const {props} = this.comp;
        const {form} = props;

        let postData = this.makeFormEditAfterOption(key);

        let res = this.postEditAfter(postData);

        if(res.success) {
            form.setAllFormValue({
                entryinfo: res.data.entryinfo
            });
        }
    }

    // 拼接完成批量新增请求参数
    makeSubmitBatchAddOption = () => {
        const {props, state} = this.comp;
        const {orgVal, form, entryApplyListPks} = props;

        let postData = {
            listpks: entryApplyListPks,
            model: form.getAllFormValue('entryinfo'),
            pk_org: orgVal.refpk
        };
        
        return postData;
    }

    // 发起完成批量新增请求
    postSubmitBatchAdd = async (postData) => {
        try {
            let res = await proFetch({
                url: '/nccloud/hrtrn/entrymng/EntryBatchAddDoAction.do',
                body: postData
            });

            return res;
        }
        catch(e) {
            console.error(e);
        }
    }

    // 批量新增弹窗点击完成
    submitBatchAdd = async () => {
        const {props, action} = this.comp;
        const {emp, form} = props;

        if(!form.isCheckNow('entryinfo')) {
            return;
        }

        let postData = this.makeSubmitBatchAddOption();
        let res = await this.postSubmitBatchAdd(postData);

        if(res && res.success) {
            this.toast({
                color: 'success',
                content: language['hi6007-000035'] // 新增成功
            });
            action.mainAct.closeModal();
            pageTo.openTo('/nccloud/resources/hrtrn/employing/employing/main/index.html', {
                appcode: '60092010',
                pagecode: '60092010p'
            });
        }
    }

    // 生成入职申请 保存提交
    submitDeriect = async(compositepostdata={}) => {
        const {props, action} = this.comp;
        const {billids, form, orgVal} = props;

        if(!form.isCheckNow('entryinfo')) {
            return;
        }

        let postData = this.makeSubmitBatchAddOption();
        if (compositepostdata && compositepostdata.content) {
            let pd2 = {
                billids: billids,
                pk_org: orgVal.refpk,
                content: compositepostdata
            }
            this.submit(pd2)
            
        } else {
            let res = await this.postSubmitBatchAdd(postData);
            if (res && res.success) {
                this.comp.props.updateState({
                    billids: res.data['billids']
                })
                let pd1 = {
                    billids: res.data['billids'],
                    pk_org: orgVal.refpk,
                }
                this.submit(pd1)
            } 
        }
    }

    // 提交
    submit = async(pd) => {
        const {props, action} = this.comp;
        let resp = await proFetch({
            url: '/nccloud/hrtrn/entrymng/EntryCommitAction.do',
            body: pd
        });
        if (resp.data.content) {
            this.comp.props.updateState({
                assignVisible: true,
                compositedata: resp.data.content
            })
           return
        } else if (resp.data.errorMsg) {
            this.toast({
                color: 'danger',
                content: resp.data.errorMsg 
            });
        } else {
            this.comp.props.updateState({
                assignVisible: false,
                compositedata: null
            })
            this.toast({
                color: 'success',
                content: language['hi6007-000298'] || '保存提交成功' // 保存提交成功
            });
            action.mainAct.closeModal();
            pageTo.openTo('/nccloud/resources/hrtrn/employing/employing/main/index.html', {
                appcode: '60092010',
                pagecode: '60092010p'
            });
        }
    }
    // 指派确定事件
    assign = (value) => {
        this.submitDeriect(value)
     }
 
     // 指派取消
     cancelAssign = () => {
         this.comp.props.updateState({
             assignVisible: false,
             compositedata: null
         })
     }
}